﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataTransferObject;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OAuth;

namespace _5ChickDeal.UserControl
{
    public partial class Login : System.Web.UI.UserControl
    {

        DataClassesDataContext db;
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DataClassesDataContext();

            if (Session["user"] != null)
            {
                this.Visible = false;
            }
            else
            {
                Visible = true;
            }

            OpenIdRelyingParty OIDRP = new OpenIdRelyingParty();
            var str = OIDRP.GetResponse();

            if (str != null)
            {
                switch (str.Status)
                {
                    case AuthenticationStatus.Authenticated:

                        var fetch = str.GetExtension<FetchResponse>();
                        string email = String.Empty;
                        String hoten = String.Empty;
                        if (fetch != null)
                        {
                            email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                            hoten = fetch.GetAttributeValue(WellKnownAttributes.Name.Last) +
                             " " + fetch.GetAttributeValue(WellKnownAttributes.Name.First);


                            var kh = from k in db.KhachHangs
                                     where k.email == email
                                     select k;

                            KhachHang kh2 = new KhachHang();
                            if (kh.Count() == 0)
                            {

                                kh2.id = getMaxKhID() + 1;
                                kh2.email = email;
                                kh2.hoten = hoten;
                                db.KhachHangs.InsertOnSubmit(kh2);
                                db.SubmitChanges();

                            }
                            else
                            {
                                kh2 = kh.First();
                            }
                            Session["user"] = kh2;
                            Session.Timeout = 30;



                        }


                        Response.Redirect((string)Session["prevpage"]);
                        break;
                    case AuthenticationStatus.Canceled:

                        break;
                    case AuthenticationStatus.Failed:

                        break;
                }
            }



        }
        private int getMaxKhID()
        {
            int id = 0;
            try
            {
                id = db.KhachHangs.Max(kh => kh.id);
            }

            catch { }
            return id;
        }

        protected void OpenLogin_Click(object src, CommandEventArgs e)
        {
            string str = e.CommandArgument.ToString();
            OpenIdRelyingParty openid = new OpenIdRelyingParty();
            var b = new UriBuilder(Request.Url) { Query = "" };
            var req = openid.CreateRequest(str, b.Uri, b.Uri);


            var fetch = new FetchRequest();
            fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
            fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
            fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
            req.AddExtension(fetch);

            Session["login"] = "google";
            req.RedirectToProvider();


        }


        protected void OpenIDYahoo_Click(object src, CommandEventArgs e)
        {
            string str = e.CommandArgument.ToString();
            OpenIdRelyingParty openid = new OpenIdRelyingParty();
            var b = new UriBuilder(Request.Url) { Query = "" };
            var req = openid.CreateRequest(str, b.Uri, b.Uri);


            var fetch = new FetchRequest();
            fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
            fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
            fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
            req.AddExtension(fetch);
            Session["login"] = "yahoo";
            req.RedirectToProvider();


        }

        protected void loginGoogle_Click(object sender, ImageClickEventArgs e)
        {
            Session["prevpage"] = "~" + Page.Request.Url.PathAndQuery;
        }
    }

}